#!/usr/bin/env python3

import requests
from bs4 import BeautifulSoup

isExcludeZero = True
isExcludeNone = True

baseUrl = 'https://coding.imooc.com/class/{}.html'
classList = []

max = int(input("Input the end number: "))

print("=== Start ===")
for i in range(max):
    if i == 0 and isExcludeZero:
        continue
    reqUrl = baseUrl.format(i)
    print("Requesting " + reqUrl)
    res = requests.get(reqUrl)
    res.encoding='UTF-8'

    soup = BeautifulSoup(res.text, 'lxml')
    try:
        title = soup.h1.get_text()
    except:
        title = "None"

    classIterm = [i, title]
    if title == "None" and isExcludeNone:
        continue
    classList.append(classIterm)
print("=== End ===")
print(classList)
print("=== Done ===")

with open("results/imooc.csv", 'w') as f:
    for iterm in classList:
        print(iterm)
        f.writelines("{}, {}\n".format(iterm[0], iterm[1]))